﻿using HPIT.RentHouse.Admin.Filters;
using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.Common;
using RentHouse.DTO;
using RentHouse.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RentHouse.Admin.Controllers
{
    [Authorize]
    public class RolesController : Controller
    {
        // GET: Roles
        private readonly IPermissionService _permissionService;
        private readonly IRolesService _rolesService;
        public RolesController(IRolesService rolesService,IPermissionService permissionService)
        {
            _permissionService = permissionService;
            _rolesService = rolesService;
        }

        //[CheckMyPermission("lookRole")]
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetList(int start, int length, string name)
        {
            //1.接口层写分页方法
            //2.service实现接口方法
            //3.控制器调用
            int rowcount = 0;
            var list = _rolesService.GetPagedList(start, length, name, ref rowcount);
            PageModel pageModel = new PageModel();
            pageModel.data = list;
            pageModel.recordsFiltered = rowcount;
            pageModel.recordsTotal = rowcount;
            return Json(pageModel);
        }

        public ActionResult Add()
        {
            List<PermissionDTO> perList = _permissionService.GetList();
            ViewBag.perList = perList;
            return View();
        }
        [HttpPost]
        public ActionResult AddRole(RoleEditDTO editDTO)
        {
            AjaxResult result = _rolesService.AddRole(editDTO);
            return Json(result);
        }

        public ActionResult Edit(long id)
        {
            ViewBag.perList = _rolesService.GetList();
            return View(_rolesService.GetEditRole(id));
        }

        [HttpPost]
        public ActionResult UpdateRole(RoleEditDTO editDTO)
        {
            AjaxResult result = _rolesService.Update(editDTO);
            return Json(result);
        }
        [HttpPost]
        public ActionResult Delete(long id)
        {
            AjaxResult result = _rolesService.Delete(id);
            return Json(result);
        }
        [HttpPost]
        public ActionResult DeleteBatch(List<long> ids)
        {
            AjaxResult result = _rolesService.DeleteBatch(ids);
            return Json(result);
        }
    }
}